/* ----------------------------------------------------------------------------------------------------------------
 *  Entity
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.entity 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	import pure.debug.Logger;
	import pure.engine.renderer.Renderer;
	import pure.engine.entity.RendererCarrier;

	import pure.engine.status.Status;
	import flash.events.IEventDispatcher;
	import pure.engine.world.WorldContent;
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair;
	
	
	
	/// @eventType	pure.engine.events.EntityMouseEvent.CLICK
	[Event(name="click", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_DOWN
	[Event(name="mouseDown", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_UP
	[Event(name="mouseUp", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_MOVE
	[Event(name="mouseMove", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_OUT
	[Event(name="mouseOut", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_OVER
	[Event(name = "mouseOver", type = "pure.engine.events.EntityMouseEvent")] 
	
	
	/**
	 * 实体
	 * [Author] - PURE
	 * 
	 */
public class Entity extends RendererCarrier
{        

	
	

	//======================
	// Property
	//====================== 
		
	
	// 名字
	public function get name():String { return m_name; }

	public function get id():int { return m_id; }
	
	// 当前所在世界
	public function get worldContent():WorldContent { return m_worldContent; }

	// 坐标X
	public function get x():Number { return m_x; }
	public function set x(v:Number):void { m_x = v; }
	
	// 坐标Y
	public function get y():Number { return m_y; }
	public function set y(v:Number):void { m_y = v; }
	
	
	// 是否可见
	override public function set visible(b:Boolean):void
	{ 
		if (m_visible != b)
		{
			m_visible = b;
		}
	}
	

	/**
	 * 弧度
	 * 
	 * [Hint] - 实体自身的朝向
	 */
	override public function set angle(v:Number):void 
	{ 
		if (m_angle != v)
		{
			m_angle = v// < 0 ? v + TWO_PI : v;
			//m_eventDispatcher.dispatchEvent(
		}
	}
	
	// 真实区域
	public function get realRect():Rectangle
	{
		m_realRect.x = _realX + m_x;
		m_realRect.y = _realY + m_y;
		return m_realRect;
	}


	
	
	
	
	//======================
	// Base
	//======================
	
	
	// 移动
	public function move(wx:Number, wy:Number):void
	{
		if (m_x != wx)    m_x  =  wx;
		if (m_y != wy)    m_y  =  wy;
	}

	
	// 真实体积
	public function setRealRect(rect:Rectangle):void
	{
		m_realRect = rect;
		_realX = rect.x;
		_realY = rect.y;
	}

	
	// 杀死
	// [Hint] - 实体visible立即变为false，60帧后自动移除
	public function kill():void
	{
		if (!m_alive)  return;
		
		destroy();
	}
	

	
	

	
	
	//======================
	// Status !!
	//======================
	
	
	public function addStatus(status:Status):void
	{	
		status.setEntity(this);
		m_statusList.push(status);
	}
	
	
	public function removeStatus(index:int):void 
	{
		var S:Status;
		
		S = m_statusList.splice(index, index);
		S.dispose();
	}

	
	public function getStatus(index:int = 0):Status
	{
		return m_statusList[index];
	}
	
	
	public function clearAllStatus():void
	{
		var l:int;
		
		l = m_statusList.length;
		while (--l > -1)
		{
			m_statusList[l].dispose();
		}
		m_statusList.length = 0;
	}


	
	
	
	//======================
	// Event Dispatcher
	//======================

	
	// 发送
	public function dispatchEvent (event:Event) : Boolean
	{
		return m_eventDispatcher.dispatchEvent(event);
	}
	
	
	// 侦听
	public function addEventListener (type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false) : void
	{
		m_eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
	}
	
	
	// 移除
	public function removeEventListener (type:String, listener:Function, useCapture:Boolean = false) : void
	{
		m_eventDispatcher.removeEventListener(type, listener, useCapture);
	}
	
	
	// 查找
	public function hasEventListener (type:String) : Boolean
	{
		return m_eventDispatcher.hasEventListener(type);
	}
	
	
	// 根查找
	public function willTrigger (type:String) : Boolean
	{
		return m_eventDispatcher.willTrigger(type);
	}
	
	
	

	
	
	/* ----------------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------------
	 */
	
	 
	 
	 
	 // 毁灭
	override ns_despair function destroy():void
	{
		super.destroy();

		EntityManager.unregister(this);
		clearAllStatus();
		m_range  =  m_x  =  m_y  =  0;
		m_rangeDirty  =  mirrorExisted  =  m_alive = m_visible = false;
		m_interactive = true;
	}
	

	
	// 生成实体
	ns_despair static function getEntity(name:String, layer:int):Entity
	{
		var E:Entity;
		
		if (numEntity > 0) 
		{
			numEntity--;
			E = cachedEntityList.pop();
		}
		else 
		{
			E = new Entity();
		}
		
		E.m_alive  =  E.m_visible  =  true;
		E.m_name   =  name;
		E.m_layer  =  layer;
		EntityManager.register(E);
		
		return E;
	}
	
	
	// 回收
	ns_despair function recycle():void
	{
		cachedEntityList[numEntity++] = this;
	}
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair static var cachedEntityList:Vector.<Entity> = new Vector.<Entity>();
	
	ns_despair static var numEntity:int;


	ns_despair var m_layer:int;
	
	ns_despair var m_worldContent:WorldContent;

	ns_despair var m_statusList:Array = [];

	ns_despair var m_alive:Boolean;  // 是否活着

	ns_despair var m_eventDispatcher:EventDispatcher = new EventDispatcher();
	


	//======================
	// Cached Object
	//======================

	
	ns_despair var mirrorExisted:Boolean;    // 残像是否已存在于监控器

	ns_despair var _realX:Number, _realY:Number;
	

	
	
	

}
}

